<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->

<div class="integrations-overview">
  <div class="page-header">
    <h1 class="page-header__page-title">Overview</h1>
    <span class="page-header__description body-strong">Monitor and manage your integration with the provider</span>
  </div>

  <mat-card *ngIf="!isLoadingIntegration && integration; else loadingTemplate">
    <mat-card-header>
      <div class="card-header">
        <div class="card-header__title">
          <h3>{{ integration.name }}</h3>
          <p *ngIf="integration.description.length" class="description">{{ integration.description }}</p>
        </div>
      </div>
    </mat-card-header>

    <div class="separator-hr"></div>

    <div class="card-content">
      <mat-card>
        <div class="details-card">
          <div class="details-card__item border-right">
            <h5 class="title body-strong">Provider</h5>
            <div class="name">
              <img class="logo" [src]="'../../../assets/logos/' + integration.provider + '.svg'" alt="Integration provider logo" />
              <span class="body-strong">{{ integration.provider }}</span>
            </div>
          </div>
          <div class="details-card__item border-right">
            <h5 class="title body-strong">Agent Connection</h5>
            <div class="status">
              <span *ngIf="integration.agentStatus === 'DISCONNECTED'; else connected" class="gio-badge-error">
                <mat-icon class="icon-left" svgIcon="gio:prohibition"></mat-icon>
                Disconnected
              </span>

              <ng-template #connected>
                <span class="gio-badge-success">
                  <mat-icon class="icon-left" svgIcon="gio:check"></mat-icon>
                  Connected
                </span>
              </ng-template>
            </div>
          </div>
          <div class="details-card__item">
            <h5 class="title body-strong">Integration ID</h5>
            <div class="status">
              <span class="gio-badge-neutral id-badge">
                <div gioClipboardCopyWrapper [alwaysVisible]="true" [contentToCopy]="integration?.id">
                  {{ integration.id }}
                </div>
              </span>
            </div>
          </div>
        </div>
      </mat-card>
      <gio-banner-error *ngIf="integration.agentStatus === 'DISCONNECTED'">
        Check your agent status and ensure connectivity with the provider to start importing your APIs in Gravitee.
      </gio-banner-error>
    </div>
  </mat-card>

  <mat-card *ngIf="!isLoadingIntegration && integration">
    <mat-card-header>
      <div [class]="federatedAPIs.length ? 'card-header' : 'card-header no-federated-apis'">
        <div class="card-header__title">
          <h3>APIs</h3>
        </div>
        <div class="card-header__actions">
          <button
            mat-raised-button
            color="primary"
            [disabled]="isIngesting || isLoadingFederatedAPI"
            data-testid="discover-button"
            (click)="ingest()"
            *gioPermission="{ anyOf: ['environment-integration-c'] }"
          >
            Discover
          </button>
        </div>
      </div>
    </mat-card-header>

    @if (federatedAPIs.length || isLoadingFederatedAPI) {
      <div class="card-table">
        <gio-table-wrapper
          [disableSearchInput]="true"
          [length]="nbTotalInstances"
          [filters]="filters"
          (filtersChange)="onFiltersChanged($event)"
        >
          <table mat-table [dataSource]="federatedAPIs" aria-label="Federated APIs table">
            <caption style="display: none">
              Table with federated APIs
            </caption>
            <ng-container matColumnDef="name">
              <th mat-header-cell *matHeaderCellDef id="name">API Name</th>
              <td mat-cell *matCellDef="let federatedAPI">
                <a [routerLink]="'../../apis/' + federatedAPI.id">
                  {{ federatedAPI.name }}
                </a>
              </td>
            </ng-container>
            <ng-container matColumnDef="actions">
              <th mat-header-cell *matHeaderCellDef id="actions">
                <ng-container *gioPermission="{ anyOf: ['environment-integration-u'] }">Actions</ng-container>
              </th>
              <td mat-cell *matCellDef="let federatedAPI">
                <button
                  *gioPermission="{ anyOf: ['environment-integration-u', 'environment-integration-d'] }"
                  [routerLink]="federatedAPI.id + '/configuration'"
                  mat-icon-button
                  aria-label="Configure federated API button"
                  matTooltip="Configure federated API"
                >
                  <mat-icon svgIcon="gio:edit-pencil"></mat-icon>
                </button>
              </td>
            </ng-container>
            <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
            <tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
            <!-- Row shown when there is no data -->
            <tr class="mat-mdc-row mdc-data-table__row" *matNoDataRow>
              <td *ngIf="isLoadingFederatedAPI" class="mat-mdc-cell mdc-data-table__cell" [attr.colspan]="displayedColumns.length">
                {{ 'Loading...' }}
              </td>
            </tr>
          </table>
        </gio-table-wrapper>
      </div>
    } @else {
      <div class="separator-hr"></div>
      <div class="card-content">
        <mat-card>
          <div class="no-integrations">
            <div class="no-integrations__img">
              <div class="background-circle">
                <mat-icon *ngIf="isIngesting" svgIcon="gio:down-circle" color="primary"></mat-icon>
                <mat-icon *ngIf="!isIngesting" svgIcon="gio:search" color="primary"></mat-icon>
              </div>
            </div>
            <div class="no-integrations__message">
              <h2 class="header">{{ isIngesting ? 'Discovery in progress' : 'No APIs created' }}</h2>
              <p class="description">
                {{
                  isIngesting
                    ? 'The process should only take a few minutes to complete. Come back shortly!'
                    : 'Create Federated APIs in Gravitee based on APIs or event streams from the external provider.'
                }}
              </p>
            </div>
          </div>
        </mat-card>
      </div>
    }
  </mat-card>

  <ng-template #loadingTemplate>
    <div class="loadingTemplate" data-testid="loader-spinner">
      <gio-loader></gio-loader>
    </div>
  </ng-template>
</div>
